class ComputersController < AdminController

  before_filter :login_required, :except => [:show, :check_computer]

  def new
    @page_title = "Creation d'un ordinateur"
    @rooms = prepare_computer_new
    respond_to do |format|
      format.html
      format.js
    end if @rooms.size > 0
  end

  def create
    @computer = Computer.new(params[:computer])
    if request.xhr?
      @room_view = Room.find(@computer.room.id)
      @rooms = Room.find :all
      @incidents = Incident.find :all, :conditions => [ 'resolved_at IS NULL' ]
    end
    flash[:notice] = "Création de l'ordinateur effectué"
    @computer.save!
    respond_to do |format|
      format.html { redirect_back_or_default :controller => :rooms, :action => 'show', :id => @computer.room.id }
      format.js
    end
  rescue ActiveRecord::RecordInvalid
    @rooms = prepare_computer_new
    respond_to do |format|
      format.html {  render :action => :new if @rooms.size > 0 }
      format.js { render :action => 'error_create.rjs' }
    end
  end

  def destroy
    redirect_to '/' unless logged_in?
    @computer = Computer.find params[:id]
    name = @computer.name
    @computer.destroy
    flash[:notice] = "Suppression de l'ordinateur #{name}"
    redirect_to '/'
  end

  def edit
    @computer = Computer.find params[:id]
    @page_title = "Edition de l'ordinateur #{@computer.name}"
    @rooms = prepare_computer_new
    respond_to do |format|
      format.html
      format.js
    end
  end

  def update
    @computer = Computer.find params[:id]
    if request.xhr?
      @room_view = Room.find(@computer.room.id)
      @rooms = Room.find :all
      @incidents = Incident.find :all, :conditions => [ 'resolved_at IS NULL' ]
    end
    if @computer.update_attributes params[:computer]
      flash[:notice] = "Modification de l'ordinateur effectué"
      respond_to do |format|
        format.html { redirect_to :controller => :rooms, :action => 'show', :id => @computer.room.id }
        format.js
      end
    else
      @rooms = prepare_computer_new
      render :action => :edit
    end
  end
  
  def show
    prepare_computer_view
    respond_to do |format|
      format.html 
      format.js 
    end
  end

  def check_computer
    prepare_computer_view
    @page_title = "Interrogation de la machine #{@computer_view.name}" if @computer_view
    @check = true
    respond_to do |format|
      format.html { render :action => :show }
      format.js {render :action => 'show.rjs'}
    end
  end

protected

  def prepare_computer_view
    begin
      @computer_view = Computer.find params[:id]
      @page_title = "Ordinateur #{@computer_view.name}"
    rescue ActiveRecord::RecordNotFound
      @computer_view = nil
      @page_title = "Ordinateur inconnu"
    end
  end

  def prepare_computer_new
    rooms = Room.find(:all).collect{|p| [p.name, p.id]}
    unless rooms.size > 0
      flash[:notice] = 'Créer une salle avant de créer un ordinateur'
      redirect_to :controller => :rooms, :action => :new
    end
    rooms
  end
end
